
//引入路由
import VueRouter from "vue-router";
import Message from "element-ui/packages/message/src/main";

//声明一个路由实例，赋值给router
let router = new VueRouter({
    //路由列表，写路由的详细规则
    routes:[
        //redirect 重定向
        {path:'/',redirect:'/index'},
        //把login.vue 组件映射到/login路径下
        {path:'/login',meta:{name:'登陆'}, component:() => import('@/page/login')},
        {
            path:'/index',meta:{name:'首页',needLogin:true},
            component:() => import('@/page/index'),
            children:[
                {path:'/discovermusic',meta:{name:'发现音乐'},component:() => import('@/page/discovermusic')},
                {path:'/myryanking',meta:{name:'排行榜'},component:() => import('@/page/myryanking')},
                {path:'/singer',meta:{name:'歌手'},component:() => import('@/page/singer')},
                {path:'/uploadmusic',meta:{name:'上传音乐'},component:() => import('@/page/uploadmusic')},
                {path:'/uploadmusic',meta:{name:'上传音乐'},component:() => import('@/page/uploadmusic')},
                {path:'/lovemusic',meta:{name:'我喜欢的音乐'},component:() => import('@/page/lovemusic')},
                {path:'/management',meta:{name:'个人信息管理'},component:() => import('@/page/management')},
                {path:'/songsheet',meta:{name:'歌单'},component:() => import('@/page/songsheet')},
            ]
        },
        {path:'/register',meta:{name:'注册'},component:() => import('@/page/register')},
        {path:'/beifen',meta:{name:'注册'},component:() => import('@/page/beifen')},
        {path:'/header',meta:{name:'头部'},component:() => import('@/page/myheader')},
        {path:'/myaside',meta:{name:'左边'},component:() => import('@/page/myaside')},
        {path:'/mymain',meta:{name:'中间'},component:() => import('@/page/mymain')},
        {path:'/myfooter',meta:{name:'底部'},component:() => import('@/page/myfooter')},
        {path: '/adminlogin',meta: {name:'管理员登陆'},component:() => import('@/page/admin/adminlogin')},
        {path:'/adminLayout',meta:{name:'管理员主页'},component:() => import('@/page/admin/adminLayout'),
           children: [
               {path: '/adminUser',meta: {name:'用户管理'},component:()=> import('@/page/admin/adminUser')},
               {path: '/adminSong',meta: {name:'用户管理'},component:()=> import('@/page/admin/adminSong')}
           ]
        },

    ],
    //设置mode为history，可以去掉路径上的/#/号
    mode:'history'
})
router.beforeEach(((to, from, next) => {
    // 根据路由中配置的meta.needLogin判断是否需要登录
    if (to.meta.needLogin) {
        // 判断TOKEN是否为空
        if (localStorage.getItem('TOKEN') == null) {
            Message.warning('请先登录！');
            // 拦截并跳转到指定页面，并携带参数redirect：从哪个页面跳转到的登录页
            next(`/login?redirect=${to.path}`);
        }
    }
    next();
}))
export  default router;